package com.pay.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.pay.beans.ReturnContent;
import com.pay.constants.RedisKeyConstants;
import com.pay.constants.RetConstants;
import com.pay.dubbo.wx.WxPosOrderServiceFacade;
import com.pay.entity.wx.WxPosOrder;
import com.pay.util.NeedOpenId;
import com.pay.util.RedisUtil;

@Controller
@RequestMapping("posOrder")
@SuppressWarnings("all")
public class WxPosOrderController {
	private Log log = LogFactory.getLog(WxPosOrderController.class);

	@Resource
	private WxPosOrderServiceFacade wxPosOrderServiceFacade;
	@RequestMapping(value = "findPosOrderList", produces = "text/html;charset=UTF-8")
	@NeedOpenId(needOpenId = true, isLoginFlag = false)
	public @ResponseBody String findPosOrderList(HttpServletRequest httpRequest,HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		String openId = httpRequest.getParameter("openId");
		String orderNo = httpRequest.getParameter("orderNo");
		String size = httpRequest.getParameter("size");
		String customerNo = RedisUtil.getValue(RedisKeyConstants.MULT_ACCOUNT_DEFAULT_OPENID_USERNO + openId);
		log.info("订单查询 getPosOrderList params=openId:" + openId + ",customerNo="+customerNo+",orderNo:" + orderNo + ",size:" + size);
		String returnStr = null;
		try {
			List<WxPosOrder> list = wxPosOrderServiceFacade.findWxPosOrderList(customerNo, orderNo, size);
			if(list == null || list.size() == 0){
				returnStr = JSON.toJSONString(new ReturnContent("01", "无交易记录"));
			}else{
				returnStr = JSON.toJSONString(new ReturnContent(RetConstants.SUCCESS, RetConstants.SUCCESS_MSG,list));
			}
			log.info("订单查询 getPosOrderList result:" + returnStr);
		} catch (Exception e) {
			log.info("订单查询异常", e);
			e.printStackTrace();
			return JSON.toJSONString(new ReturnContent(RetConstants.SYSTEM_ERROR, RetConstants.EXCEPTION_ERROR_MSG));
		}
		return returnStr;
	}
	
	@RequestMapping(value = "findPosOrderDetail", produces = "text/html;charset=UTF-8")
	@NeedOpenId(needOpenId = true, isLoginFlag = false)
	public @ResponseBody String findPosOrderDetail(HttpServletRequest httpRequest,HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		String openId = httpRequest.getParameter("openId");
		String orderNo = httpRequest.getParameter("orderNo");
		String customerNo = RedisUtil.getValue(RedisKeyConstants.MULT_ACCOUNT_DEFAULT_OPENID_USERNO + openId);
		log.info("订单详情查询 findPosOrderDetail params=openId:" + openId + ",customerNo="+customerNo+ ",orderNo:" + orderNo);
		String returnStr = null;
		try {
			WxPosOrder wxPosOrder = wxPosOrderServiceFacade.findPosOrderDetail(customerNo, orderNo);
			returnStr = JSON.toJSONString(new ReturnContent(RetConstants.SUCCESS, RetConstants.SUCCESS_MSG,wxPosOrder));
			log.info("订单详情查询 findPosOrderDetail result:" + returnStr);
		} catch (Exception e) {
			log.info("订单详情查询异常", e);
			e.printStackTrace();
			return JSON.toJSONString(new ReturnContent(RetConstants.SYSTEM_ERROR, RetConstants.EXCEPTION_ERROR_MSG));
		}
		return returnStr;
	}
}
